package com.yuju.controller.group;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.enums.StatusEnum;
import com.yuju.group.dao.entity.Hotel;
import com.yuju.group.enums.HotelReturnCodeEnum;
import com.yuju.group.enums.MemberLevelReturnCodeEnum;
import com.yuju.group.model.CityModel;
import com.yuju.group.model.HotelModel;
import com.yuju.group.model.ProvinceModel;
import com.yuju.group.model.TownModel;
import com.yuju.group.service.IHotelService;
import com.yuju.util.Utils;
import com.yuju.vo.group.HotelVO;
/**
 * 
 * 类名：HotelController  <br />
 *
 * 功能：
 *
 * @author : sunyl <br />
 * @Date : 2017年11月24日 上午9:40:05  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("hotel")
public class HotelController extends BaseController<HotelVO, HotelModel> {
	@Autowired
	private IHotelService hotelService;
	
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject jsonObject)   {
		HotelVO hotelVO = jsonObject.toJavaObject(HotelVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(hotelVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		hotelVO.setIsGroup(StatusEnum.FALSE.getValue());
		Result result = new Result();
		PageModel pageModel = hotelService.pageList(convert(hotelVO), hotelVO.getPage(), hotelVO.getRows());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_QUERY_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	@RequestMapping("listPure")
	@ResponseBody
	public Result listPure(@RequestBody JSONObject jsonObject)   {
		HotelVO hotelVO = jsonObject.toJavaObject(HotelVO.class);
		// 参数校验
		hotelVO.setIsGroup(StatusEnum.FALSE.getValue());
		Result result = new Result();
		List<HotelModel> list = hotelService.list(convert(hotelVO));
		if (Utils.isNotNull(list) && list.size() > 0) {
			result.setData(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_QUERY_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject jsonObject) {
		HotelVO hotelVO = jsonObject.toJavaObject(HotelVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(hotelVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = hotelService.save(convert(hotelVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_SAVE_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_SAVE_FAILURE.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject jsonObject) {
		HotelVO hotelVO = jsonObject.toJavaObject(HotelVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(hotelVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Map<String, String> failSave = FieldValidator.validateAll(hotelVO, "save", true);
		if (!failSave.isEmpty()) {
			return validateError(failSave);
		}
		Result result = new Result();
		int rows = hotelService.updateSelective(convert(hotelVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_UPDATE_FAILURE.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_UPDATE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 停业操作
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月9日 下午3:30:33 <br/>
	 */
	@RequestMapping("updateStatusForStop")
	@ResponseBody
	public Result updateStatusForStop(@RequestBody JSONObject jsonObject) {
		HotelVO hotelVO = jsonObject.toJavaObject(HotelVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(hotelVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = hotelService.updateStatusForStop(hotelVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_STOP_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_STOP_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_STOP_FAILURE.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_STOP_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 恢复营业操作
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月9日 下午3:30:41 <br/>
	 */
	@RequestMapping("updateStatusForRecover")
	@ResponseBody
	public Result updateStatusForRecover(@RequestBody JSONObject jsonObject) {
		HotelVO hotelVO = jsonObject.toJavaObject(HotelVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(hotelVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = hotelService.updateStatusForRecover(hotelVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_RECOVER_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_RECOVER_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_RECOVER_FAILURE.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_RECOVER_FAILURE.getDesc());
		}
		return result;
	}

	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject jsonObject) {
		HotelVO hotelVO = jsonObject.toJavaObject(HotelVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(hotelVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = hotelService.del(hotelVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_DEL_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_DEL_FAILURE.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_DEL_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 重命名验证
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月21日 下午8:17:11 <br/>
	 */
	@RequestMapping("validRepeatName")
	@ResponseBody
	public Result validRepeatName(@RequestBody JSONObject jsonObject) {
		HotelVO hotelVO = jsonObject.toJavaObject(HotelVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(hotelVO, "repeat", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		boolean b = hotelService.validRepeatName(hotelVO.getId(),hotelVO.getName(),hotelVO.getGroupId());
		if (b) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		return result;
	}

	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject jsonObject) {
		HotelVO hotelVO = jsonObject.toJavaObject(HotelVO.class);
		Result result = new Result();
		HotelModel GroupModel = hotelService.find(convert(hotelVO));
		if (Utils.isNotNull(GroupModel)) {
			result.setData(GroupModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_QUERY_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 获取酒店编码
	 * 功能：<br/>
	 * @param groupId
	 * @author sunyl
	 * @Date 2017年12月9日 上午11:51:01 <br/>
	 */
	@RequestMapping("getHotelCode")
	@ResponseBody
	public Result getHotelCode(@RequestBody JSONObject jsonObject) {
		HotelVO hotelVO = jsonObject.toJavaObject(HotelVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(hotelVO, "getHotelCode", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		String  hotelCode = hotelService.getHotelCode(hotelVO.getGroupId());
		if (Utils.isNotNull(hotelCode)) {
			result.setData(hotelCode);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_CODE_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_CODE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：分店下拉列表<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月7日 下午2:06:23 <br/>
	 */
	@RequestMapping("hotelDropdownList")
	@ResponseBody
	public Result hotelDropdownList(@RequestBody JSONObject obj){
		HotelVO hotelVO = obj.toJavaObject(HotelVO.class);
		List<HotelModel> list = hotelService.list(convert(hotelVO));
		Result result = new Result();
		result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
		result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
		result.setData(list);
		result.setReturnCode(MemberLevelReturnCodeEnum.MERBERLEVEL_QUERY_SUCCESS.getCode());
		result.setReturnMsg(MemberLevelReturnCodeEnum.MERBERLEVEL_QUERY_SUCCESS.getDesc());
		return result;
	}
	/**
	 * 省份列表
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月29日 下午4:10:21 <br/>
	 */
	@RequestMapping("selectProvinceList")
	@ResponseBody
	public Result selectProvinceList(@RequestBody JSONObject jsonObject) {
		Result result = new Result();
		Map<Object, Object> map = new HashMap<>();
		List<ProvinceModel> finds = hotelService.selectProvinceList(map);
		if (Utils.isNotNull(finds) && finds.size() > 0) {
			result.setData(finds);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_QUERY_PROVINCE_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_QUERY_PROVINCE_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
		
	}
	/**
	 * 地级市
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月29日 下午4:13:14 <br/>
	 */
	@RequestMapping("selectCityList")
	@ResponseBody
	public Result selectCityList(@RequestBody JSONObject jsonObject) {
		Result result = new Result();
		// 参数校验
		String provinceCode = jsonObject.getString("provinceCode");
		if(provinceCode == null){
			Map<String, String> fail = new HashMap<>();
			fail.put("provinceCode", "不能为空");
			if (!fail.isEmpty()) {
				return validateError(fail);
			}
		}
		Map<Object, Object> map = new HashMap<>();
		map.put("provinceCode", provinceCode);
		List<CityModel> finds = hotelService.selectCityList(map);
		if (Utils.isNotNull(finds) && finds.size() > 0) {
			result.setData(finds);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_QUERY_CITY_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_QUERY_CITY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
		
	}
	/**
	 * 县级市列表
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月29日 下午4:16:03 <br/>
	 */
	@RequestMapping("selectTownList")
	@ResponseBody
	public Result selectTownList(@RequestBody JSONObject jsonObject) {
		Result result = new Result();
		// 参数校验
		String cityCode = jsonObject.getString("cityCode");
		if(cityCode == null){
			Map<String, String> fail = new HashMap<>();
			fail.put("cityCode", "不能为空");
			if (!fail.isEmpty()) {
				return validateError(fail);
			}
		}
		Map<Object, Object> map = new HashMap<>();
		map.put("cityCode", cityCode);
		List<TownModel> finds = hotelService.selectTownList(map);
		if (Utils.isNotNull(finds) && finds.size() > 0) {
			result.setData(finds);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_QUERY_TOWN_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_QUERY_TOWN_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
		
	}
	/**
	 * 地级市
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月29日 下午4:13:14 <br/>
	 */
	@RequestMapping("selectHotelCity")
	@ResponseBody
	public Result selectHotelCity(@RequestBody JSONObject jsonObject) {
		Result result = new Result();
		// 参数校验
		Map<Object, Object> map = new HashMap<>();
		List<CityModel> finds = hotelService.selectHotelCity(map);
		if (Utils.isNotNull(finds) && finds.size() > 0) {
			result.setData(finds);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_QUERY_CITY_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_QUERY_CITY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
		
	}
	/**
	 * 县级市列表
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月29日 下午4:16:03 <br/>
	 */
	@RequestMapping("selectHotelTown")
	@ResponseBody
	public Result selectHotelTown(@RequestBody JSONObject jsonObject) {
		Result result = new Result();
		// 参数校验
		String cityName = jsonObject.getString("cityName");
		if(cityName == null){
			Map<String, String> fail = new HashMap<>();
			fail.put("cityName", "不能为空");
			if (!fail.isEmpty()) {
				return validateError(fail);
			}
		}
		Map<Object, Object> map = new HashMap<>();
		map.put("cityName", cityName);
		List<TownModel> finds = hotelService.selectHotelTown(map);
		if (Utils.isNotNull(finds) && finds.size() > 0) {
			result.setData(finds);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(HotelReturnCodeEnum.Hotel_QUERY_TOWN_SUCCESS.getCode());
			result.setReturnMsg(HotelReturnCodeEnum.Hotel_QUERY_TOWN_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
		
	}
	
}
